package com.aoxiang.zbox.config;

import com.aoxiang.zbox.config.filter.CorsFilter;
import com.aoxiang.zbox.config.filter.SetDataSourceFilter;
import com.aoxiang.zbox.config.filter.TokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description:
 *
 * @author: aoxiang
 * @create: 2024-12-28 11:00
 **/

@Configuration
public class FilterConfig {

    @Autowired
    private SetDataSourceFilter setDataSourceFilter;
    @Autowired
    private CorsFilter corsFilter;
    @Autowired
    private TokenFilter tokenFilter;

    @Bean
    public FilterRegistrationBean<CorsFilter> addCorsFilter() {
        FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(corsFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<TokenFilter> addTokenFilter() {
        FilterRegistrationBean<TokenFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(tokenFilter);
        registrationBean.addUrlPatterns("/api/*");
        registrationBean.setOrder(2);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<SetDataSourceFilter> addSetDataSourceFilter() {
        FilterRegistrationBean<SetDataSourceFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(setDataSourceFilter);
        registrationBean.addUrlPatterns("/api/*");
        registrationBean.setOrder(3);
        return registrationBean;
    }

}
